ImageCodec : Multi-format image encoder and decoder
This module provides codecs that support multiple formats of image. Currently supported image formats include: 'PNG'
, 'JPG'
, 'BMP'
, 'TGA'
, 'HDR'
.
User can use the following code to import the imagecodec
module.
var imagecodec = require('imagecodec');
Support
The following shows imagecodec
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
imagecodec.decode | ● | ● |
imagecodec.encode | ● | ● |
imagecodec.resize | ● | ● |
imagecodec.info | ● | ● |
ImageCodec Module
imagecodec.decode(path[, opt])
path
{String} Image file path.opt
{Object} Decode options.- Returns: {Object} Image pixel object.
Decode the specified image and return the image pixel data.
The image decoding opt
parameter can contain the following members:
components
{Integer} Image components.
Valid components
can choose the following definitions:
Definition | Value | Description |
---|---|---|
imagecodec.COMPONENTS_DEFAULT | 0 | Use the default value of the image |
imagecodec.COMPONENTS_GREY | 1 | Single-byte grayscale image. |
imagecodec.COMPONENTS_GREY_ALPHA | 2 | Grayscale image with Alpha channel. |
imagecodec.COMPONENTS_RGB | 3 | Three-byte RGB image. |
imagecodec.COMPONENTS_RGB_ALPHA | 4 | RGB image with Alpha channel. |
The returned object contains the following members:
width
{Integer} Image width.height
{Integer} Image height.components
{Integer} Image components bytes.buffer
{Buffer} Image pixel data buffer.
Example
var image = imagecodec.decode('./test.png');
imagecodec.decode(buffer[, opt])
buffer
{Buffer} Image file data buffer.opt
{Object} Decode options.- Returns: {Object} Image pixel object.
Decode the specified image and return the image pixel data.
Example
var image = imagecodec.decode(buffer);
imagecodec.encode(image, path[, opt])
image
{Object} Image pixel object.path
{String} Destination path.opt
{Object} Encode options.- Returns: {Boolean} Whether the operation was successful.
Compress the original pixel data of the image and store it in the specified path.
The image
object must contain the following members:
width
{Integer} Image width.height
{Integer} Image height.components
{Integer} Image components bytes.buffer
{Buffer} Image pixel data buffer.
The path
parameter determines the image encoding format, it must contain the file extensions supported by the current module, they include: '*.jpg'
, '*.png'
, '*.bmp'
, '*.tga'
, '*.hdr'
.
The opt
parameter is optional, it can contain the following members:
stride
{Integer} If the target is inPNG
format, the number ofstride
bytes can be specified here, please refer to here for details.quality
{Integer} If the target isJPEG
format, quality can be specified here, optional range: 1 ~ 100. default: 70.
Example
imagecodec.encode(image, './test.jpg', { quality: 80 });
imagecodec.encode(image, format[, opt])
image
{Object} Image pixel object.format
{String} Image format.opt
{Object} Encode options.- Returns: {Buffer} Image data after encoding.
Compress the original pixel data of the image and store it in buffer.
format
specifies the image encoding format, currently supported encoding formats include: 'jpg'
, 'png'
, 'bmp'
, 'tga'
, 'hdr'
.
Example
var buf = imagecodec.encode(image, 'jpg', { quality: 80 });
imagecodec.resize(image, dest)
image
{Object} Source image pixel object.dest
{Object} The image size you want to convert to.- Returns: {Object} Image pixel object.
Image conversion.
The image
object must contain the following members:
width
{Integer} Image width.height
{Integer} Image height.components
{Integer} Image components bytes.buffer
{Buffer} Image pixel data buffer.
The dest
object must contain the following members:
width
{Integer} Image width.height
{Integer} Image height.components
{Integer} Image components bytes.
The returned object contains the following members:
width
{Integer} Image width.height
{Integer} Image height.components
{Integer} Image components bytes.buffer
{Buffer} Image pixel data buffer.
Example
var newImage = imagecodec.resize(image, {
width: 640,
height: 480,
components: imagecodec.COMPONENTS_GREY
});
imagecodec.info(path)
path
{String} Image file path.- Returns: {Object} Image pixel object.
Get image size information.
The returned object contains the following members:
width
{Integer} Image width.height
{Integer} Image height.components
{Integer} Image components bytes.
imagecodec.info(buffer)
buffer
{Buffer} Image file data buffer.- Returns: {Object} Image pixel object.
Get image size information. The internal storage of buffer
is image data with format (jpg
, png
...).
The returned object contains the following members:
width
{Integer} Image width.height
{Integer} Image height.components
{Integer} Image components bytes.